package com.drinks.yiniang.adapter.community;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatTextView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.banner.CommunityActBannerAdapter;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.entity.community.CommunityActJson;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.Constants;
import com.hansen.library.ui.widget.banner.recycler.RecyclerBanner2Layout;
import com.hansen.library.ui.widget.image.CircleImageView;
import com.hansen.library.ui.widget.layout.AutoLineFeedLayout;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.ScreenSizeUtils;
import com.hansen.library.utils.StringUtils;

import java.util.List;

/**
 * 社区活动列表adapter
 * @author han
 * @date 2019/8/30
 */
public class CommunityActAdapter extends BaseQuickRCVAdapter<CommunityActJson.CommunityActList, BaseViewHolder> {

    private AppCompatTextView mTextView;

    private Drawable mTagDrawable;
    private Drawable mCollectNormalDrawable;
    private Drawable mCollectFocusDrawable;
    private Drawable mLikeNormalDrawable;
    private Drawable mLikeFocusDrawable;

    private ViewGroup.LayoutParams params;

    private CommunityActBannerAdapter mBannerAdapter;

    private SpannableStringBuilder mBuilder;

    private String[] mTagArray;

    private int dp8, dp3;
    private int start, end;

    public CommunityActAdapter(Context mContext, @Nullable List<CommunityActJson.CommunityActList> data) {
        super(R.layout.item_community_act, data);

        mTagDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_tag);

        dp8 = ScreenSizeUtils.dp2px(mContext, 8);
        dp3 = ScreenSizeUtils.dp2px(mContext, 3);

        params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,  ScreenSizeUtils.dp2px(mContext, 22));

        mBuilder = new SpannableStringBuilder();

        mCollectNormalDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_wishlist_n);
        mCollectFocusDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_wishlist_p);

        mLikeNormalDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_like_n);
        mLikeFocusDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_like_p);
    }

    @Override
    protected void convert(BaseViewHolder helper, CommunityActJson.CommunityActList item) {

        GlideUtils.loadAvatar(mContext, (CircleImageView) helper.getView(R.id.iv_community_act_avatar), item.getStores_avatar());
        //banner
        setBannerData(helper, item.getImages_list());
        //昵称
        helper.setText(R.id.tv_community_act_nickname, item.getStores_name());
        //标题
        helper.setText(R.id.tv_community_act_title, item.getContent());
        //标签
        getLabelView((AutoLineFeedLayout) helper.getView(R.id.auto_feed_community_act_label), item.getTag());
        //地址
        helper.setText(R.id.tv_community_act_address, item.getStores_address());
        //活动时间
        mBuilder.clearSpans();
        mBuilder.clear();
        mBuilder.append(mContext.getString(R.string.text_activity_time_colon));
        mBuilder.append(StringUtils.getNullEmptyConvert__(item.getStart_time()));
        mBuilder.append("~");
        mBuilder.append(StringUtils.getNullEmptyConvert__(item.getEnd_time()));
        helper.setText(R.id.tv_community_act_book_time, mBuilder);
        //收藏点赞
        setCollectAndLikeData((TextView) helper.getView(R.id.tv_community_act_collect), (TextView) helper.getView(R.id.tv_community_act_like), item);
        //评论数
        helper.setText(R.id.tv_community_act_total_comments, String.format(mContext.getString(R.string.text_total_comment_format), StringUtils.getNullEmptyConvertZero(item.getComment_count())));
        //评论
        getCommentView((LinearLayout) helper.getView(R.id.ll_community_act_comments), item.getComment());
        //时间
        helper.setText(R.id.tv_community_act_time, item.getCreated_time());

        //添加监听
        helper.addOnClickListener(R.id.iv_community_act_three_dot);
        helper.addOnClickListener(R.id.tv_community_act_collect, R.id.tv_community_act_like);
        helper.addOnClickListener(R.id.tv_community_act_total_comments, R.id.tv_community_act_comments_add);
    }

    private void setBannerData(BaseViewHolder helper, List<String> images) {
        RecyclerBanner2Layout banner = helper.getView(R.id.rcv_community_act_banner);

        mBannerAdapter = new CommunityActBannerAdapter(mContext);
        mBannerAdapter.addBannerData(images);
        banner.setBannerAdapter(mBannerAdapter);
        banner.start();
    }

    private void getLabelView(AutoLineFeedLayout label, String tagStr) {
        label.removeAllViews();

        if(StringUtils.isEmpty(tagStr)) {
            return;
        }

        mTagArray = tagStr.split(",");

        if(mTagArray == null || mTagArray.length == 0) {
            return;
        }

        for(String tag : mTagArray) {
            mTextView = new AppCompatTextView(mContext);
            mTextView.setPadding(dp8, 0, dp8, 0);
            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            mTextView.setTextColor(ColorUtils.getColorById(mContext, R.color.color_3d6bd8));
            mTextView.setGravity(Gravity.CENTER_VERTICAL);
            mTextView.setCompoundDrawablesWithIntrinsicBounds(mTagDrawable, null, null, null);
            mTextView.setBackgroundResource(R.drawable.shape_bkg_follow_label);
            mTextView.setCompoundDrawablePadding(dp3);
            mTextView.setText(StringUtils.getNullEmptyConvert__(tag));

            label.addView(mTextView, params);
        }
    }

    private void setCollectAndLikeData(TextView tvCollect, TextView tvLike, CommunityActJson.CommunityActList item) {
        //收藏数
        tvCollect.setText(StringUtils.getNullEmptyConvertZero(item.getCollect_count()));
        tvCollect.setCompoundDrawablesWithIntrinsicBounds(Constants.STATUS_GLOBAL_YES.equals(item.getIs_collect()) ? mCollectFocusDrawable : mCollectNormalDrawable, null, null, null);
        //点赞数
        tvLike.setText(StringUtils.getNullEmptyConvertZero(item.getLike_count()));
        tvLike.setCompoundDrawablesWithIntrinsicBounds(Constants.STATUS_GLOBAL_YES.equals(item.getIs_like()) ? mLikeFocusDrawable : mLikeNormalDrawable, null, null, null);
    }

    private void getCommentView(LinearLayout ll, List<CommunityActJson.CommunityActComments> comments) {
        ll.removeAllViews();

        if(CommonUtils.isEmptyList(comments)) {
            return;
        }

        for(int i = 0; i < comments.size(); i++) {
            mBuilder.clearSpans();
            mBuilder.clear();
            mBuilder.append(StringUtils.getNullEmptyConvert__(comments.get(i).getNickname()));
            end = mBuilder.length();
            mBuilder.append(" ");
            mBuilder.append(StringUtils.getNullEmptyConvert__(comments.get(i).getContent()));
            mBuilder.setSpan(new StyleSpan(Typeface.BOLD), 0, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

            mTextView = new AppCompatTextView(mContext);
            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            mTextView.setTextColor(ColorUtils.getColorById(mContext, R.color.color_282829));
            mTextView.setLines(1);
            mTextView.setEllipsize(TextUtils.TruncateAt.END);
            mTextView.setText(mBuilder);

            ll.addView(mTextView);
        }
    }
}
